import time
from Common.Main_page import MainPage
from PageLocators.ALL_locator import loc
from Common.handle_log import do_log

class PerformancePage(MainPage):

    # """01 退出、进入播放  循环30次"""
    def Play_001_Entry_Exit_player(self):
        """01 退出、进入播放  循环30次"""
        do_log.info("""PLAY_001 退出、进入播放  循环30次""")
        self.wake_seekbar()
        self.click_element(loc.play_back_loc, "步骤PLAY_001播放页-点击播放页返回按钮")
        time.sleep(4)
        self.click_element(loc.search_playNo1, "步骤PLAY_001搜索结果页-点击播放第一集")
        time.sleep(8)
        if self.judge_ele_exists(loc.play_back_loc):
            time.sleep(10)


    # """02 快进三次、快退三次  循环10次"""
    def Play_002_Seek(self):
        """02 快进三次、快退三次  循环10次"""
        do_log.info("""PLAY_002 快进三次、快退三次  循环10次""")
        for i in range(3):
            self.swip_right("步骤PLAY_002 播放页快进")
            time.sleep(2)
        for i in range(3):
            self.swip_left("步骤PLAY_002 播放页快退")
            time.sleep(2)

    # """03 分别选3、2、1集 循环10次"""
    def Play_003_Switch_anthologys(self):
        """03 分别选3、2、1集 循环10次"""
        do_log.info("""PLAY_003 分别选3、2、1集 循环10次""")
        anthologys = [
            "3",
            "2",
            "1"
        ]
        for rate in anthologys:
            self.wake_seekbar()
            if self.judge_ele_exists(loc.tv_anthology_loc):
                time.sleep(0.5)
                self.click_element(loc.tv_anthology_loc, "步骤PLAY_003_播放页_点击选集按钮")
            else:
                self.wake_seekbar()
                time.sleep(0.5)
                self.click_element(loc.tv_anthology_loc, "步骤PLAY_003_播放页_点击选集按钮_第二次尝试")
            time.sleep(2)
            self.click_element(self.get_text_loc(rate), f"步骤PLAY_003_播放页选集列表页_点击选择第'{rate}'集进行播放")
            if self.judge_ele_exists(self.get_text_loc(rate)):
                self.click_element(self.get_text_loc(rate),
                                          f"步骤PLAY_003_播放页选集列表页_点击选择第'{rate}'集进行播放")
                time.sleep(8)

            time.sleep(8)
            if self.judge_ele_exists(loc.tv_loading_loc):
                time.sleep(5)
        time.sleep(6)

    # """04所有倍速切换一遍 循环10次"""
    def Play_004_Switch_speeds(self):
        """04所有倍速切换一遍 循环10次"""
        do_log.info("""PLAY_004 所有倍速切换一遍 循环10次""")
        anthologys = [
            "0.75X",
            "2.0X",
            "1.5X",
            "1.25X",
            "1.0X",
        ]
        for rate in anthologys:
            self.wake_seekbar()
            if not self.judge_ele_exists(loc.tv_speed_loc):
                time.sleep(7)
                self.wake_seekbar()
            self.click_element(loc.tv_speed_loc, "步骤PLAY_004_播放页_点击倍速按钮")
            time.sleep(2)
            self.click_element(self.get_text_loc(rate), f"NOTPLAY_004_播放页倍速列表页_点击选择'{rate}'速度进行播放")
            time.sleep(4)
        time.sleep(6)

    # """05所有分辨率切换一遍 循环10次"""
    def Play_005_Switch_rates(self):
        """05所有分辨率切换一遍 循环10次"""
        do_log.info("""PLAY_005 所有分辨率切换一遍 循环10次""")
        anthologys = [
            "标清480P",
            "准高清720P",
            "高清1080P"
        ]
        for rate in anthologys:
            self.wake_seekbar()
            if not self.judge_ele_exists(loc.tv_bit_rate_loc):
                time.sleep(7)
                self.wake_seekbar()
            self.click_element(loc.tv_bit_rate_loc, "步骤PLAY_005_播放页_点击清晰度按钮")
            time.sleep(2)
            self.click_element(self.get_text_loc(rate),
                                      f"步骤PLAY_005_播放页清晰度列表页_点击选择'{rate}'清晰度进行播放")
            time.sleep(8)

    # """06视频模式切换音频模式，音频模式再切换回视频模式 循环10次"""
    def Play_006_Switch_mode(self):
        """06视频模式切换音频
        模式，音频模式再切换回视频模式 循环10次"""
        do_log.info("""PLAY_006 视频模式切换音频模式 循环10次""")
        if loc.audio_loading_loc:
            self.wake_seekbar()
            self.click_element(loc.audio_mode_loc, "步骤PLAY_006_播放页_点击音频模式")
            time.sleep(1)
            i = 20
            while i > 0 and self.judge_ele_exists(loc.audio_loading_loc):
                i -= 1
                time.sleep(2)
            time.sleep(2)
            self.click_element(loc.tv_change_video_loc, "步骤PLAY_006_播放页_点击视频模式")
            time.sleep(8)
        else:
            do_log.info("步骤PLAY_006_无音频模式按钮，跳过")

    # """所有频道切换一次，切换到新的频道后上滑三次 循环3次"""
    def NOT_PLAY_001_step(self):
        """所有频道切换一次，切换到新的频道后上滑三次 循环3次"""
        do_log.info("""NOTPLAY_001 所有频道切换一次，切换到新的频道后上滑三次 循环3次""")
        channels = [self.get_text_loc("电视剧"),
                    self.get_text_loc("推荐"),
                    self.get_text_loc("电影"),
                    self.get_text_loc("综艺"),
                    self.get_text_loc("云影院"),
                    self.get_text_loc("杜比专区"),
                    self.get_text_loc("动漫"),
                    self.get_text_loc("纪录片"),
                    self.get_text_loc("儿童"),
                    ]
        for channel in channels:
            if self.judge_ele_exists(channel):
                time.sleep(0.5)
                self.click_element(channel, "步骤NOTPLAY_001_主页_点击进入 {} 频道".format(channel.split('"')[1]))
                time.sleep(2)
                for i in range(0, 3):
                    self.swip_up("步骤NOTPLAY_001_上滑")
                    time.sleep(1)

        time.sleep(2)


    def NOT_PLAY_002_step(self):
        """点击进入"个人中心"，分别点击进入"观看历史"、"我的收藏"、"设置"并退出，最后回到首页，循环10次"""
        do_log.info("""NOTPLAY_002 点击进入"个人中心"，分别点击进入"观看历史"、"我的收藏"、"设置"并退出，最后回到首页，循环10次""")
        self.click_element(loc.main_enter_mine_Loc, "主页_点击个人中心")
        time.sleep(2)
        channels = {"历史记录": loc.mine_history_loc,
                    "我的收藏": loc.mine_collect_loc,
                    "设置": loc.mine_setting_loc
                    }
        channels_back = {"历史记录": loc.mine_history_back_loc,
                         "我的收藏": loc.mine_collect_back_loc,
                         "设置": loc.mine_setting_back_loc
                         }

        for channel in channels.keys():
            if not self.judge_ele_exists(channels[channel]):
                time.sleep(0.5)
                self.swip_up("NOTPLAY_002_个人中心页面")
            self.click_element(channels[channel], "步骤NOTPLAY_002_个人中心页_点击进入 {} 频道".format(channel))
            time.sleep(1)
            if self.judge_ele_exists(channels_back[channel]):
                time.sleep(0.5)
                self.click_element(channels_back[channel],"步骤NOTPLAY_002_点击返回")
                time.sleep(1)
        self.click_element(loc.mine_back_loc, "步骤NOTPLAY_002_个人中心页返回频道")
        time.sleep(2)


    def NOT_PLAY_003_step(self):
        """#分别点击进入"电视剧"、"电影"、"综艺"频道，连续上滑至底部回到顶部   循环3次"""
        do_log.info("""NOTPLAY_003 #分别点击进入"电视剧"、"电影"、"综艺"频道，连续上滑至底部回到顶部   循环3次""")
        channels = [self.get_text_loc("综艺"),
                    self.get_text_loc("电视剧"),
                    self.get_text_loc("电影"),
                    ]
        for channel in channels:
            self.click_element(channel, "步骤NOTPLAY_003_主页_点击进入 {} 频道".format(channel.split('"')[1]))
            time.sleep(1)
            i = 15
            while i > 0 and not self.judge_ele_exists(self.get_text_loc("暂无更多内容哦")) :
                i-=1
                self.swip_up("步骤NOTPLAY_003_首页上滑")
                time.sleep(1)
            time.sleep(1)
            if loc.main_top_loc:
                if self.judge_ele_exists(loc.main_top_loc):
                    self.click_element(loc.main_top_loc,"步骤NOTPLAY_003_点击置顶")
                    time.sleep(1)
            elif loc.main_top_point_loc:
                x,y=loc.main_top_point_loc
                self.click_point(x,y,img_desc="步骤NOTPLAY_003_置顶")
            else:
                pass


    def NOT_PLAY_004_step(self):
        """ 首页点击进入"搜索"并回到首页，点击进入"历史记录"后回到首页，循环10次"""
        do_log.info( """NOTPLAY_004 首页点击进入"搜索"并回到首页，点击进入"历史记录"后回到首页，循环10次""")
        pagename = self.get_page_name()
        if not (pagename == "首页"):
            do_log.error(f"004筛选页回到首页失败，通过adb指令回到首页,当前错误页面为{pagename}")
            self.enter_to_main_page()
            time.sleep(3)
        if loc.main_enter_search_Loc:
            self.click_search()
            time.sleep(1)
            self.click_search_back()
        else:
            do_log.info("首页无进入搜索按钮,跳过")
        if loc.main_enter_history_Loc:
            self.click_history()
            time.sleep(1)
            self.click_history_back()
        else:
            do_log.info("首页无历史记录按钮,跳过")


    def NOT_PLAY_005_step(self):
        """进入“电视剧》爱奇艺精品自制剧”，“电影》最新上线”，“综艺》好看的真人秀综艺” 切换标签，筛选页上滑到底部，并回到顶部， 循环3次"""
        do_log.info("""NOTPLAY_005 进入“电视剧》爱奇艺精品自制剧”，“电影》最新上线”，“综艺》好看的真人秀综艺” 切换标签，筛选页上滑到底部，并回到顶部， 循环3次"""
                    )
        channels_list={"电影":"最新上线",
                       "综艺": "好看的真人秀综艺",
                       "电视剧": "爱奇艺精品自制剧",
                       }
        for channel in channels_list.keys():
            channel1=self.get_text_loc(channel)
            time.sleep(1)

            self.click_element(channel1, f"步骤NOTPLAY_005_主页_点击进入 {channel} 频道")
            channel2=self.get_text_loc(channels_list[channel])
            channel3 = self.get_text_loc(channel+"风云榜")
            time.sleep(1)

            if not self.judge_ele_exists(channel2):
                if loc.main_top_loc:
                    if self.judge_ele_exists(loc.main_top_loc):
                        self.click_element(loc.main_top_loc,"步骤NOTPLAY_005_点击置顶按钮")

                if not self.judge_ele_exists(channel3):
                    i = 20
                    while i > 0 and not self.judge_ele_exists(channel3):
                        self.swip_up("步骤NOTPLAY_005_搜索结果页面上滑")
                        time.sleep(1)
                        i = i - 1
                if self.judge_ele_exists(channel3):
                    i=10
                    while i>0 and not self.judge_ele_exists(channel2):
                        self.swip_up("步骤NOTPLAY_005_搜索结果页面上滑",during=850)
                        time.sleep(1)
                        i=i-1
            time.sleep(1)
            self.click_element(channel2, f"步骤NOTPLAY_005_主页_点击进入 {channels_list[channel]} 频道")
            j = 5
            while self.judge_ele_exists(loc=self.get_text_loc("内容即将呈现")):
                time.sleep(5)
                j -= 1
            i = 10
            pagename=self.get_page_name()
            if pagename !="筛选页":
                do_log.error(f"步骤NOTPLAY_005_当前页面不在筛选页,在{pagename}请注意")
            else:
                while i > 0 and self.judge_ele_exists(self.get_text_loc("暂无更多内容哦")) == False:
                    i -= 1
                    self.swip_up("步骤NOTPLAY_005_筛选页上滑")
                    time.sleep(1)
                if loc.main_top_loc:
                    if self.judge_ele_exists(loc.main_top_loc):
                        self.click_element(loc.main_top_loc, "步骤NOTPLAY_005_点击置顶按钮")
                elif loc.main_top_point_loc:
                    x,y=loc.main_top_point_loc
                    self.click_point_by_score(x,y,img_desc="置顶")
                else:
                    pass
                time.sleep(1)
                self.click_element(loc.main_shaixuan_back,"步骤NOTPLAY_005_筛选页面返回")

    def NOT_PLAY_006_step(self):
        """006 进入风云榜 “电视剧”，“电影”，“综艺”，上滑到底部，并回到顶部， 循环3次"""
        do_log.info("""NOTPLAY_006 进入风云榜 “电视剧”，“电影”，“综艺”，上滑到底部，并回到顶部， 循环3次""")
        self.click_text_loc("推荐")
        text_loc=self.get_text_loc("风云榜")
        text_loc1 = self.get_text_loc("猜你喜欢")
        time.sleep(1)
        if not self.judge_ele_exists(text_loc):
            if loc.main_top_loc:
                if self.judge_ele_exists(loc.main_top_loc):
                    self.click_element(loc.main_top_loc, "步骤NOTPLAY_006_点击置顶按钮")
                    time.sleep(1)
            if not self.judge_ele_exists(text_loc):
                i = 20
                while i > 0 and not self.judge_ele_exists(text_loc1):
                    self.swip_down(during=80,img_desc="步骤NOTPLAY_006_风云榜页面下滑")
                    time.sleep(1)
                    i = i - 1
            if self.judge_ele_exists(text_loc1):
                i = 10
                while i > 0 and not self.judge_ele_exists(text_loc):
                    self.swip_up(img_desc="步骤NOTPLAY_006_风云榜页面上滑")
                    time.sleep(1)
                    i = i - 1
        time.sleep(3)
        self.click_text_loc("风云榜")
        pagename=self.get_page_name()
        if pagename != "风云榜":
            do_log.error(f"NOTPLAY_006_当前页面不在风云榜页面,在{pagename}")
        else:
            channels = [
                self.get_text_loc("综艺"),
                self.get_text_loc("电影"),
                self.get_text_loc("电视剧"),
            ]
            for channel in channels:
                self.click_element(channel, "风云榜_点击进入 {} 频道".format(channel.split('"')[1]))
                time.sleep(1)
                j = 5
                while self.judge_ele_exists(loc=self.get_text_loc("内容即将呈现")):
                    time.sleep(5)
                    j -= 1
                if not self.judge_ele_exists(self.get_text_loc("爱奇艺风云榜")):
                    do_log.error("爱奇艺风云榜执行失败")
                    continue
                i = 50
                while i > 0 and self.judge_ele_exists(self.get_text_loc("暂无更多内容哦")) == False:
                    i -= 1
                    self.swip_up("步骤NOTPLAY_006_风云榜页面上滑")
                    time.sleep(1)
            if self.judge_ele_exists(loc.main_fengyun_back):
                self.click_element(loc.main_fengyun_back, "步骤NOTPLAY_006_点击风云榜返回按钮")
                time.sleep(1)


    def Play_001(self,number,flag,test_mode=False):
        if test_mode:
            number=test_mode
        if not flag:
            pagename=self.get_page_name()
            do_log.error(f"001_当前页面为{pagename},不在播放页，跳过")
            return None
        for j in range(number):  # 退出、进入播放 循环30次
            try:
                do_log.info(f'001 退出进入播放第{j+1}次^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^')
                self.Play_001_Entry_Exit_player()
            except:
                do_log.error(f'001 退出进入播放第{j+1}失败')
                pass
    def Play_002(self, number, flag,test_mode=False):
        if test_mode:
            number=test_mode
        if not flag:
            pagename=self.get_page_name()
            do_log.error(f"当前页面为{pagename},不在播放页，跳过")
            return None

        for j in range(number):  # 快进、快退 循环10次
            try:
                do_log.info(
                    '002 播放页快进快退 第' + str(j+1) + '次^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^')
                self.Play_002_Seek()
            except:
                do_log.error(f'002 播放页快进快退第{j + 1}失败')
                pass
    def Play_003(self, number, flag,test_mode=False):
        if test_mode:
            number=test_mode
        if not flag:
            pagename=self.get_page_name()
            do_log.error(f"当前页面为{pagename},不在播放页，跳过")
            return None

        for j in range(number):  # 选集 循环10次
            try:
                do_log.info(
                    '003 播放页切换选集 第' + str(j+1) + '次^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^')
                self.Play_003_Switch_anthologys()
            except:
                do_log.error(f'003 播放页切换选集第{j + 1}失败')
                pass
    def Play_004(self, number, flag,test_mode=False):
        if test_mode:
            number=test_mode
        if not flag:
            pagename=self.get_page_name()

            do_log.error(f"当前页面为{pagename},不在播放页，跳过")
            return None

        for j in range(number):  # 切换倍速 循环10次
            try:
                do_log.info(
                    '004 播放页切换倍速 第' + str(j+1) + '次^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^')
                self.Play_004_Switch_speeds()
            except:
                do_log.error(f'004 播放页切换倍速第{j + 1}失败')
                pass
    def Play_005(self, number, flag,test_mode=False):
        if test_mode:
            number=test_mode
        if not flag:
            pagename=self.get_page_name()

            do_log.error(f"当前页面为{pagename},不在播放页，跳过")
            return None

        for j in range(number):  # 切换分辨率 循环10次
            try:
                do_log.info(
                    '005 播放页切换分辨率 第' + str(j+1) + '次^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^')
                self.Play_005_Switch_rates()
            except:
                do_log.error(f'005 播放页切换分辨率第{j + 1}失败')
                pass
    def Play_006(self, number, flag,test_mode=False):
        if test_mode:
            number=test_mode
        if not flag:
            pagename=self.get_page_name()

            do_log.error(f"当前页面为{pagename},不在播放页，跳过")
            return None

        for j in range(number):  # 切换播放模式 循环10次
            try:
                do_log.info(
                    '006 播放页切换播放模式 第' + str(j) + '次^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^')
                self.Play_006_Switch_mode()
            except:
                pass

    def NOT_Play_001(self, number, flag,test_mode=False):
        if test_mode:
            number=test_mode
        if not flag:
            pagename=self.get_page_name()
            do_log.error(f"当前页面为{pagename},不在首页，跳过")
            return None

        for j in range(number):
            # 所有频道切换一次，切换到新的频道后上滑三次 循环3次
            try:
                do_log.info(
                    '011 主页所有频道上滑三次 第' + str(j) + '次^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^')
                pagename = self.get_page_name()
                if not (pagename == "首页"):
                    do_log.error(f"011回到首页失败，通过adb指令回到首页,当前错误页面为{pagename}")
                    self.enter_to_main_page()
                    time.sleep(3)
                self.NOT_PLAY_001_step()
            except:
                pass

    def NOT_Play_002(self, number, flag,test_mode=False):
        if test_mode:
            number=test_mode
        if not flag:
            pagename=self.get_page_name()
            do_log.error(f"当前页面为{pagename},不在首页，跳过")
            return None

        for j in range(number):
            # """点击进入"个人中心"，分别点击进入"观看历史"、"我的收藏"、"设置"并退出，最后回到首页，循环10次"""
            try:
                do_log.info(
                    '012 进入个人中心 第' + str(j+1) + '次^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^')
                pagename = self.get_page_name()
                if not (pagename == "首页"):
                    do_log.error(f"012回到首页失败，通过adb指令回到首页,当前错误页面为{pagename}")
                    self.enter_to_main_page()
                    time.sleep(3)
                self.NOT_PLAY_002_step()
            except:
                pass
    def NOT_Play_003(self, number, flag,test_mode=False):
        if test_mode:
            number=test_mode
        if not flag:
            pagename=self.get_page_name()
            do_log.error(f"当前页面为{pagename},不在首页，跳过")
            return None

        for j in range(number):
            # """#分别点击进入"电视剧"、"电影"、"综艺"频道，连续上滑至底部回到顶部   循环3次"""
            try:
                do_log.info(
                    '013 电视剧电影综艺 频道下滑到底 第' + str(j) + '次^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^')
                pagename = self.get_page_name()
                if not (pagename == "首页"):
                    do_log.error(f"013回到首页失败，通过adb指令回到首页,当前错误页面为{pagename}")
                    self.enter_to_main_page()
                    time.sleep(3)
                self.NOT_PLAY_003_step()
            except:
                pass

    def NOT_Play_004(self, number, flag,test_mode=False):
        if test_mode:
            number=test_mode
        if not flag:
            pagename=self.get_page_name()
            do_log.error(f"当前页面为{pagename},不在首页，跳过")
            return None

        for j in range(number):
            # 首页点击进入"搜索"并回到首页，点击进入"历史记录"后回到首页，循环10次
            try:
                do_log.info('014 主页进入搜索和历史记录 第' + str(
                    j) + '次^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^')
                pagename = self.get_page_name()
                if not (pagename == "首页"):
                    do_log.error(f"014筛选页回到首页失败，通过adb指令回到首页,当前错误页面为{pagename}")
                    self.enter_to_main_page()
                    time.sleep(3)
                self.NOT_PLAY_004_step()
            except:
                pass

    def NOT_Play_005(self, number, flag,test_mode=False):
        if test_mode!=False:
            number=test_mode
        if not flag:
            pagename = self.get_page_name()
            do_log.error(f"当前页面为{pagename},不在首页，跳过")
            return None

        for j in range(number):
            # """进入“电视剧》爱奇艺精品自制剧”，“电影》最新上线”，“综艺》好看的真人秀综艺” 切换标签，筛选页上滑到底部，并回到顶部， 循环3次"""
            try:
                do_log.info('015 进入 电视剧 电影 综艺 筛选页 第' + str(
                    j) + '次^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^')
                pagename = self.get_page_name()
                if not (pagename == "首页"):
                    do_log.error(f"015回到首页失败，通过adb指令回到首页,当前错误页面为{pagename}")
                    self.enter_to_main_page()
                    time.sleep(3)
                self.NOT_PLAY_005_step()
            except:
                pass
    def NOT_Play_006(self, number, flag,test_mode=False):
        if test_mode:
            number=test_mode
        if not flag:
            pagename=self.get_page_name()
            do_log.error(f"当前页面为{pagename},不在首页，跳过")
            return None

        for j in range(number):
            #  """006 进入风云榜 “电视剧”，“电影”，“综艺”，上滑到底部，并回到顶部， 循环3次"""
            try:
                do_log.info('016 进入风云榜 “电视剧”，“电影”，“综艺”，上滑到底部，并回到顶部， 循环3次 第' + str(
                    j) + '次^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^')
                pagename = self.get_page_name()
                if not (pagename == "首页"):
                    do_log.error(f"016回到首页失败，通过adb指令回到首页,当前错误页面为{pagename}")
                    self.enter_to_main_page()
                    time.sleep(3)
                self.NOT_PLAY_006_step()
            except:
                pass









